import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO(model = "runs/detect/train/weights/best.pt")
# Open the video file
video_path = "D:/fire.mp4"
cap = cv2.VideoCapture(video_path)

# Loop through the video frames
while cap.isOpened():
    # 从视频中读取一帧,
    success, frame = cap.read()
    if success:
        results = model(frame)
        #可视化视频帧上的结果
        annotated_frame = results[0].plot()
        # 显示带注释的视频帧
        cv2.imshow("YOLOv12 Inference", annotated_frame)
        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
